#include "widget.h"
#include "ui_widget.h"
#include <QGridLayout>
#include <QLabel>
#include <QVBoxLayout>

#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QMessageBox>
#include <QDir>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , m_pLayout(new QVBoxLayout(parent))
{
    ui->setupUi(this);
    mVecAck.clear();
    mVecBid.clear();
    initUI();

}

Widget::~Widget()
{
    delete ui;
}

void Widget::loadStyle(const QString &fileName)
{
    QFile file(fileName);
    if(file.open(QFile::ReadOnly)) {
        qApp->setStyleSheet(file.readAll());
        file.close();
    }
}
void Widget::initUI() {
    QPalette pal(this->palette());
     pal.setColor(QPalette::Background, QColor("#FFFFFF"));
     this->setAutoFillBackground(true);
     this->setPalette(pal);

    initStkInfo();
    initData();
    initGearInfo();
    setLayout(m_pLayout);

    loadStyle(":/stk_style.qss");
}

void Widget::initStkInfo() {

    QString styleLabel = " color: #333B47; \
            font-size: 22px; \
            font-weight: bold; \
            font-family: Arial; ";
    QWidget *headerInfo = new QWidget(this);
    QGridLayout *pHeaderLayout = new QGridLayout(this);
    mLblStkCode = new QLabel(this);
    mLblStkCode->setText(tr("00700"));
    mLblStkCode->setStyleSheet(styleLabel);
    pHeaderLayout->addWidget(mLblStkCode, 0, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblStkName = new QLabel(this);
    mLblStkName->setText(tr("腾讯控股"));
    mLblStkName->setStyleSheet(styleLabel);
    pHeaderLayout->addWidget(mLblStkName, 0, 1, 1, 4, Qt::AlignLeft|Qt::AlignVCenter);
    mLblStkPrice = new QLabel(this);
    mLblStkPrice->setText(tr("306.200"));
    pHeaderLayout->addWidget(mLblStkPrice, 1, 0, 1, 2, Qt::AlignLeft|Qt::AlignVCenter);
    mLblStkFluctuation = new QLabel(this);
    mLblStkFluctuation->setText(tr("-2.600"));
    pHeaderLayout->addWidget(mLblStkFluctuation, 1, 1, 1, 1, Qt::AlignLeft|Qt::AlignBottom);
    mLblstkFluctuationRate = new QLabel(this);
     mLblstkFluctuationRate->setText(tr("-0.800%"));
    pHeaderLayout->addWidget(mLblstkFluctuationRate, 1, 2, 1, 1, Qt::AlignLeft|Qt::AlignBottom);

    pHeaderLayout->setContentsMargins(1, 1, 1, 1);
    headerInfo->setLayout(pHeaderLayout);
    headerInfo->setFixedHeight(60);
    m_pLayout->addWidget(headerInfo);

    QWidget *stkInfoWidget = new QWidget(this);
    int iRow = 0;
    QGridLayout * pLayout = new QGridLayout();
    mLblHightName = new QLabel(this);
    mLblHightName->setText(tr("最高价"));
    pLayout->addWidget(mLblHightName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblHightName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    mLblHighVal = new QLabel(this);
    mLblHighVal->setText(tr("--"));
    pLayout->addWidget(mLblHighVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblHighVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    mLblOpenName = new QLabel(this);
    mLblOpenName->setText("开盘价");
    mLblOpenName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblOpenName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblOpenVal = new QLabel(this);
    mLblOpenVal->setText("--");
    mLblOpenVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblOpenVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblVolumeName = new QLabel(this);
    mLblVolumeName->setText(tr("成交量"));
    mLblVolumeName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblVolumeName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblVolumeVal = new QLabel(this);
    mLblVolumeVal->setText(tr("--"));
    mLblVolumeVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblVolumeVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblLowName = new QLabel(this);
    iRow++;
    mLblLowName->setText(tr("最低价"));
    mLblLowName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblLowName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblLowVal = new QLabel(this);
    mLblLowVal->setText(tr("--"));
    mLblLowVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblLowVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblPreCloseName =  new QLabel(this);
    mLblPreCloseName->setText(tr("昨收价"));
    mLblPreCloseName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblPreCloseName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblPreCloseVal = new QLabel(this);
    mLblPreCloseVal->setText(tr("--"));
    mLblPreCloseVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblPreCloseVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblTurnoverName = new QLabel(this);
    mLblTurnoverName->setText(tr("成交额"));
    mLblTurnoverName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblTurnoverName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblTurnoverVal = new QLabel(this);
    mLblTurnoverVal->setText(tr("--"));
    mLblTurnoverVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblTurnoverVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblAvgPriceVal = new QLabel(this);
    iRow++;
    mLblAvgPriceVal->setText(tr("平均价"));
    mLblAvgPriceVal->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblAvgPriceVal, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblAvgPriceName = new QLabel(this);
    mLblAvgPriceName->setText(tr("--"));
    mLblAvgPriceName->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblAvgPriceName, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblPETTMName = new QLabel(this);
    mLblPETTMName->setText(tr("市盈率TTM"));
    mLblPETTMName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblPETTMName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblPETTMVal = new QLabel(this);
    mLblPETTMVal->setText(tr("--"));
    mLblPETTMVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblPETTMVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblMarketCapName = new QLabel(this);
    mLblMarketCapName->setText(tr("总市值"));
    mLblMarketCapName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblMarketCapName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblMarketCapVal = new QLabel(this);
    mLblMarketCapVal->setText(tr("--"));
    mLblMarketCapVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblMarketCapVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblAmplitudName = new QLabel(this);
    iRow++;
    mLblAmplitudName->setText(tr("振幅"));
    mLblAmplitudName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblAmplitudName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblAmplitudVal = new QLabel(this);
    mLblAmplitudVal->setText(tr("--"));
    mLblAmplitudVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblAmplitudVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblPEStaticName = new QLabel(this);
    mLblPEStaticName->setText(tr("市盈率静"));
    mLblPEStaticName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblPEStaticName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblPeStaticVal = new QLabel(this);
    mLblPeStaticVal->setText(tr("--"));
    mLblPeStaticVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblPeStaticVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblSharesOutstandingName = new QLabel(this);
    mLblSharesOutstandingName->setText(tr("总股本"));
    mLblSharesOutstandingName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblSharesOutstandingName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblSharesOutstandingVal = new QLabel(this);
    mLblSharesOutstandingVal->setText(tr("--"));
    mLblSharesOutstandingVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblSharesOutstandingVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblTurnoverRatioName = new QLabel(this);
    iRow++;
    mLblTurnoverRatioName->setText(tr("换手率"));
    mLblTurnoverRatioName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblTurnoverRatioName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblTurnoverRatioVal = new QLabel(this);
    mLblTurnoverRatioVal->setText(tr("--"));
    mLblTurnoverRatioVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblTurnoverRatioVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblPBName = new QLabel(this);
    mLblPBName->setText(tr("市净率"));
    mLblPBName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblPBName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblPBVal = new QLabel(this);
    mLblPBVal->setText(tr("--"));
    mLblPBVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblPBVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblFloatCapName = new QLabel(this);
    mLblFloatCapName->setText(tr("流通值"));
    mLblFloatCapName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblFloatCapName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblFloatCapVal = new QLabel(this);
    mLblFloatCapVal->setText(tr("--"));
    mLblFloatCapVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblFloatCapVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLbl52WkHighName = new QLabel(this);
    iRow++;
    mLbl52WkHighName->setText(tr("52周最高"));
    mLbl52WkHighName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLbl52WkHighName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLbl52WkHighVal = new QLabel(this);
    mLbl52WkHighVal->setText(tr("--"));
    mLbl52WkHighVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLbl52WkHighVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblBidAskName = new QLabel(this);
    mLblBidAskName->setText(tr("委比"));
    mLblBidAskName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblBidAskName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblBidAskVal = new QLabel(this);
    mLblBidAskVal->setText(tr("--"));
    mLblBidAskVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblBidAskVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblShsFloatName = new QLabel(this);
    mLblShsFloatName->setText(tr("流通股"));
    mLblShsFloatName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblShsFloatName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblShsFloatVal = new QLabel(this);
    mLblShsFloatVal->setText(tr("--"));
    mLblShsFloatVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblShsFloatVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLbl52WkLowName = new QLabel(this);
    iRow++;
    mLbl52WkLowName->setText(tr("52周最低"));
    mLbl52WkLowName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLbl52WkLowName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLbl52WkLowVal = new QLabel(this);
    mLbl52WkLowVal->setText(tr("--"));
    mLbl52WkLowVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLbl52WkLowVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblVolumeRatioName = new QLabel(this);
    mLblVolumeRatioName->setText(tr("量比"));
    mLblVolumeRatioName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblVolumeRatioName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblVolumeRatioVal = new QLabel(this);
    mLblVolumeRatioVal->setText(tr("--"));
    mLblVolumeRatioVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblVolumeRatioVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblMinTradingUnitName = new QLabel(this);
    mLblMinTradingUnitName->setText(tr("每手"));
    mLblMinTradingUnitName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblMinTradingUnitName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblMinTradingUnitVal = new QLabel(this);
    mLblMinTradingUnitVal->setText(tr("--"));
    mLblMinTradingUnitVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblMinTradingUnitVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblHistoryLowName = new QLabel(this);
    iRow++;
    mLblHistoryLowName->setText(tr("历史最低"));
    mLblHistoryLowName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblHistoryLowName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblHistoryLowVal = new QLabel(this);
    mLblHistoryLowVal->setText(tr("--"));
    mLblHistoryLowVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblHistoryLowVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblDividendTTMName = new QLabel(this);
    mLblDividendTTMName->setText(tr("股息TTM"));
    mLblDividendTTMName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblDividendTTMName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblDividendTTMVal = new QLabel(this);
    mLblDividendTTMVal->setText(tr("--"));
    mLblDividendTTMVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblDividendTTMVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblDivYieldTTMName = new QLabel(this);
    mLblDivYieldTTMName->setText(tr("股息率TTM"));
    mLblDivYieldTTMName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblDivYieldTTMName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblDivYieldTTMVal = new QLabel(this);
    mLblDivYieldTTMVal->setText(tr("--"));
    mLblDivYieldTTMVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblDivYieldTTMVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblHistoryHighName = new QLabel(this);
    mLblHistoryHighName->setText(tr("历史最高"));
    mLblHistoryHighName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    iRow++;
    pLayout->addWidget(mLblHistoryHighName, iRow, 0, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblHistroyHighVal = new QLabel(this);
    mLblHistroyHighVal->setText(tr("--"));
    mLblHistroyHighVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblHistroyHighVal, iRow, 1, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblDividendLFYName = new QLabel(this);
    mLblDividendLFYName->setText(tr("股息LFY"));
    mLblDividendLFYName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblDividendLFYName, iRow, 2, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblDividendLFYVal = new QLabel(this);
    mLblDividendLFYVal->setText(tr("--"));
    mLblDividendLFYVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblDividendLFYVal, iRow, 3, 1, 1, Qt::AlignRight|Qt::AlignVCenter);
    mLblDivYieldLFYName = new QLabel(this);
    mLblDivYieldLFYName->setText(tr("股息率LFY"));
    mLblDivYieldLFYName->setStyleSheet(" color: #6F7680;\
                                  font-size: 14px;\
                                  padding: 1px;");
    pLayout->addWidget(mLblDivYieldLFYName, iRow, 4, 1, 1, Qt::AlignLeft|Qt::AlignVCenter);
    mLblDivYieldLFYVal = new QLabel(this);
    mLblDivYieldLFYVal->setText(tr("--"));
    mLblDivYieldLFYVal->setStyleSheet(" color: #333B47; font-size: 14px; font-weight: bold; padding: 1px;");
    pLayout->addWidget(mLblDivYieldLFYVal, iRow, 5, 1, 1, Qt::AlignRight|Qt::AlignVCenter);

    pLayout->setSpacing(1);

  // 设置外间距
    pLayout->setContentsMargins(1, 1, 1, 1);
    stkInfoWidget->setLayout(pLayout);


    m_pLayout->addWidget(stkInfoWidget);


   // m_pLayout->setMargin(5);

//    // 设置水平间距
//    pLayout->setHorizontalSpacing(1);
//    // 设置垂直间距
//    pLayout->setVerticalSpacing(1);



}

void Widget::initGearInfo(void) {

    QWidget *stkGearWidget = new QWidget(this);
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    QWidget *bidPlate = gearInfo(10, m_vecBidData, true);
    QWidget *askPlate = gearInfo(10, m_vecAskData, false);
    bidPlate->setStyleSheet("background-color: #FEF9F9;");
    askPlate->setStyleSheet("background-color: #F7FCF9;");
    pLayout->addWidget(bidPlate);
    pLayout->addWidget(askPlate);
    stkGearWidget->setLayout(pLayout);

    m_pLayout->addWidget(stkGearWidget);
}

QWidget* Widget::gearInfo(int num, const QVector<std::shared_ptr<bid_ask_item_t>> &items, bool type ) {
    if (num > items.size()) {

        return nullptr;
    }
    QWidget *plateWidget  = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(this);

    for (int i = 0; i<num; i++) {
        StkLevelItem * item = new StkLevelItem(this);
        item->update(items[i], type);
        layout->addWidget(item);
        if(type) {
            mVecBid.push_back(item);
        } else {
            mVecAck.push_back(item);
        }
    }
    plateWidget->setLayout(layout);
    return plateWidget;
}

bool Widget::initData() {
   // QString strHomePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
  //  QString path = QDir::currentPath();
    QFile file(":/stk_info.json");
    QString path = QCoreApplication::applicationDirPath();
    if (!file.open(QIODevice::ReadOnly)) {
        // 处理文件打开失败的情况
        QMessageBox::information(this, "MessageInfo", "Open file faild...");
        return false;
    }
    QByteArray fileContent = file.readAll();

    QJsonParseError error;    //解析错误反馈
       QJsonDocument jsonDoc=QJsonDocument::fromJson(fileContent,&error);
       if(error.error==QJsonParseError::NoError){
           QJsonObject obj=jsonDoc.object(); //Json键值对
       }else  {
        // JSON解析失败，处理错误情况
        return false;
    }
    QJsonObject jsonObject = jsonDoc.object();
    if(!jsonObject.contains("code")) {
        return false;
    }
    int nCode = jsonObject.value("code").toInt();
    if(nCode != 200) {
        QString sMsg = jsonObject.value("msg").toString();
        return false;
    }
    if(!jsonObject.contains("data")) {
        return false;
    }
    QJsonObject dataObject = jsonObject.value("data").toObject();
    if(!dataObject.contains("i")) {
        return false;
    }

    QJsonObject infoObject = dataObject.value("i").toObject();

    if(infoObject.contains("closingPrice")) {
        QString sEndPrice = infoObject.value("closingPrice").toString();
        mLblPreCloseVal->setText(sEndPrice);
        mPreClosePrice = sEndPrice.toDouble();
    }

    if(infoObject.contains("code")) {
        QString sStkCode = infoObject.value("code").toString();
        mLblStkCode->setText(sStkCode);
    }

    if(infoObject.contains("name")) {
        QString sStkName = infoObject.value("name").toString();
        mLblStkName->setText(sStkName);
    }

    if(infoObject.contains("realTimePrice")) {
        QString sStkPrice = infoObject.value("realTimePrice").toString();
        mLblStkPrice->setText(sStkPrice);
        if(mPreClosePrice < sStkPrice.toDouble()){
            mLblStkPrice->setStyleSheet(" color: #DB1A21;\
                                          font-size: 24px;\
                                          font-weight: bold;\
                                          padding: 1px;");
        } else if(mPreClosePrice > sStkPrice.toDouble()) {
            mLblStkPrice->setStyleSheet(" color: #00A843;\
                                          font-size: 24px;\
                                          font-weight: bold;\
                                          padding: 1px;");
        }
    }

    if(infoObject.contains("todayPriceChange")) {
        QString sStkPriceChange = infoObject.value("todayPriceChange").toString();

        if(mPreClosePrice < sStkPriceChange.toDouble()){
            mLblStkFluctuation->setStyleSheet(" color: #DB1A21;\
                                          font-size: 18px;\
                                          font-weight: bold;\
                                          padding: 1px;");
             mLblStkFluctuation->setText(sStkPriceChange);
        } else if(mPreClosePrice > sStkPriceChange.toDouble()) {
            mLblStkFluctuation->setStyleSheet(" color: #00A843;\
                                          font-size: 18px;\
                                          font-weight: bold;\
                                          padding: 1px;");
            mLblStkFluctuation->setText(sStkPriceChange);
        }
    }

    if(infoObject.contains("todayPriceChangeRate")) {
        QString sStkPriceChangeRate = infoObject.value("todayPriceChangeRate").toString();
        if(mPreClosePrice < sStkPriceChangeRate.toDouble()){
            mLblstkFluctuationRate->setStyleSheet(" color: #DB1A21;\
                                          font-size: 18px;\
                                          font-weight: bold;\
                                          padding: 1px;");
            mLblstkFluctuationRate->setText(  sStkPriceChangeRate+"%");
        } else if(mPreClosePrice > sStkPriceChangeRate.toDouble()) {
            mLblstkFluctuationRate->setStyleSheet(" color: #00A843;\
                                          font-size: 18px;\
                                          font-weight: bold;\
                                          padding: 1px;");
             mLblstkFluctuationRate->setText(  sStkPriceChangeRate+"%");        }
    }
    if(infoObject.contains("todayHigh")) {
        QString sTodayHigh = infoObject.value("todayHigh").toString();

        if(mPreClosePrice < sTodayHigh.toDouble()){
            mLblHighVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");
             mLblHighVal->setText(sTodayHigh);
        } else if(mPreClosePrice > sTodayHigh.toDouble()) {
            mLblHighVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");
              mLblHighVal->setText(sTodayHigh);
        }
    }
    if(infoObject.contains("todayOpen")) {
        QString sTodayOpen = infoObject.value("todayOpen").toString();
        if(mPreClosePrice < sTodayOpen.toDouble()){
            mLblOpenVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > sTodayOpen.toDouble()) {
            mLblOpenVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLblOpenVal->setText(sTodayOpen);
    }
    if(infoObject.contains("todayVolume")) {
        QString sTodayVolume = QString::number((infoObject.value("todayVolume").toInt() / 10000.00),'f', 2) + "万";
        mLblVolumeVal->setText(sTodayVolume);
    }
    if(infoObject.contains("todayLow")) {
        QString sTodayLow = infoObject.value("todayLow").toString();
        if(mPreClosePrice < sTodayLow.toDouble()){
            mLblLowVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > sTodayLow.toDouble()) {
            mLblLowVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLblLowVal->setText(sTodayLow);
    }


    //
    if(infoObject.contains("todayTurnover")) {
        QString sTodayTurnover = infoObject.value("todayTurnover").toString();

        sTodayTurnover = QString::number((sTodayTurnover.toDouble() / 100000000.00),'f', 2) + "亿";
        mLblTurnoverVal->setText(sTodayTurnover);
    }
    //
    if(infoObject.contains("avgPrice")) {
        QString sAvgPrice = infoObject.value("avgPrice").toString();
        if(mPreClosePrice < sAvgPrice.toDouble()){
            mLblAvgPriceName->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > sAvgPrice.toDouble()) {
            mLblAvgPriceName->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLblAvgPriceName->setText(sAvgPrice);
    }
    //
    if(infoObject.contains("peTTM")) {
        QString sPETTM = infoObject.value("peTTM").toString();
        mLblPETTMVal->setText(sPETTM);
    }

    if(infoObject.contains("marketValueTotal")) {
        QString sMarketValueTotal = infoObject.value("marketValueTotal").toString();
        sMarketValueTotal = QString::number((sMarketValueTotal.toDouble() / 1000000000000.00),'f', 2) + "万亿";
        mLblMarketCapVal->setText(sMarketValueTotal);
         mLblFloatCapVal->setText(sMarketValueTotal);
    }
    //
    if(infoObject.contains("swing")) {
        QString sSwing = infoObject.value("swing").toString();
        mLblAmplitudVal->setText(sSwing+"%");
    }
    //
    if(infoObject.contains("peLFY")) {
        QString sPeLFY = infoObject.value("peLFY").toString();
        mLblPeStaticVal->setText(sPeLFY);
    }

    if(infoObject.contains("totalCapital")) {
        QString sTotalCapital = infoObject.value("totalCapital").toString();
        sTotalCapital  = QString::number((sTotalCapital.toDouble() / 100000000.00),'f', 2) + "亿";
        mLblSharesOutstandingVal->setText(sTotalCapital);
    }

    if(infoObject.contains("turnoverRate")) {
        QString sTurnoverRate = infoObject.value("turnoverRate").toString();
        mLblTurnoverRatioVal->setText(sTurnoverRate+"%");
    }
    //
    if(infoObject.contains("quantityRelativeRatio")) {
        QString sQuantityRelativeRatio = infoObject.value("quantityRelativeRatio").toString();
        mLblVolumeRatioVal->setText(sQuantityRelativeRatio);
    }

    if(infoObject.contains("lotsize")) {
        int sLotSize = infoObject.value("lotsize").toInt();
        QString sLotPer = QString("%1股").arg(sLotSize);
        mLblMinTradingUnitVal->setText(sLotPer);
    }

    if(infoObject.contains("highIn52week")) {
        QString highIn52week = infoObject.value("highIn52week").toString();
        if(mPreClosePrice < highIn52week.toDouble()){
            mLbl52WkHighVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > highIn52week.toDouble()) {
            mLbl52WkHighVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLbl52WkHighVal->setText(highIn52week);
    }

    if(infoObject.contains("newCapital")) {
        QString newCapital = infoObject.value("newCapital").toString();
        newCapital  = QString::number((newCapital.toDouble() / 100000000.00),'f', 2) + "亿";
        mLblShsFloatVal->setText(newCapital);
    }
    //
    if(infoObject.contains("lowIn52week")) {
        QString lowIn52week = infoObject.value("lowIn52week").toString();
        if(mPreClosePrice < lowIn52week.toDouble()){
            mLbl52WkLowVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > lowIn52week.toDouble()) {
            mLbl52WkLowVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLbl52WkLowVal->setText(lowIn52week);
    }

    if(infoObject.contains("lowInHistory")) {
        QString lowInHistory = infoObject.value("lowInHistory").toString();
        if(mPreClosePrice < lowInHistory.toDouble()){
            mLblHistoryLowVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > lowInHistory.toDouble()) {
            mLblHistoryLowVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLblHistoryLowVal->setText(lowInHistory);
    }

    if(infoObject.contains("highInHistory")) {
        QString highInHistory = infoObject.value("highInHistory").toString();
        if(mPreClosePrice < highInHistory.toDouble()){
            mLblHistroyHighVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > highInHistory.toDouble()) {
            mLblHistroyHighVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLblHistroyHighVal->setText(highInHistory);
    }

    if(infoObject.contains("todayLow")) {
        QString sTodayLow = infoObject.value("todayLow").toString();
        if(mPreClosePrice < sTodayLow.toDouble()){
            mLblLowVal->setStyleSheet(" color: #DB1A21;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        } else if(mPreClosePrice > sTodayLow.toDouble()) {
            mLblLowVal->setStyleSheet(" color: #00A843;\
                                          font-size: 14px;\
                                          font-weight: bold;\
                                          padding: 1px;");

        }
        mLblLowVal->setText(sTodayLow);
    }

//    if(infoObject.contains("todayLow")) {
//        QString sTodayLow = infoObject.value("todayLow").toString();
//        mLblLowVal->setText(sTodayLow);
//    }

    if(infoObject.contains("dividendLFY")) {
        QString dividendLFY = infoObject.value("dividendLFY").toString();
        mLblDividendLFYVal->setText(dividendLFY);
    }

    if(infoObject.contains("dividendTTM")) {
        QString dividendTTM = infoObject.value("dividendTTM").toString();
        mLblDividendTTMVal->setText(dividendTTM);
    }
    if(infoObject.contains("bookRatios")) {
        QString PBVal = infoObject.value("bookRatios").toString();
        mLblPBVal->setText(PBVal);
    }
    if(infoObject.contains("commissionRatio")) {
        QString commissionRatio = infoObject.value("commissionRatio").toString();
        mLblBidAskVal->setText(commissionRatio+"%");
    }


    if(!dataObject.contains("p")) {
        return false;
    }
    QJsonObject objGear = dataObject.value("p").toObject();
    if(objGear.contains("b")) {
        QJsonArray arrBid = objGear.value("b").toArray();
        for (int i = 0; i < arrBid.count(); i++)
        {
              std::shared_ptr<bid_ask_item_t> data = std::make_shared<bid_ask_item_t>();
               QJsonObject itemObject = arrBid.at(i).toObject();
               if(itemObject.contains("p")) {
                   data->index = QString("%1").arg(i+1);
                   data->mPrice = itemObject.value("p").toString();
                   data->mQty = QString("%1").arg(itemObject.value("q").toInt());
                   data->mBrokerNum = QString("%1").arg(itemObject.value("n").toInt());
                   m_vecBidData.push_back(data);
               }
        }
    }

    if(objGear.contains("s")) {
        QJsonArray arrAsk = objGear.value("s").toArray();
        for (int i = 0; i < arrAsk.count(); i++)
        {
              std::shared_ptr<bid_ask_item_t> data = std::make_shared<bid_ask_item_t>();
               QJsonObject itemObject = arrAsk.at(i).toObject();
               if(itemObject.contains("p")) {
                   data->index = QString("%1").arg(i+1);
                   data->mPrice = itemObject.value("p").toString();
                   data->mQty = QString("%1").arg(itemObject.value("q").toInt());
                   data->mBrokerNum = QString("%1").arg(itemObject.value("n").toInt());
                   m_vecAskData.push_back(data);
               }
        }
    }

    file.close();

    return true;
}
